﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class InstallSucceed : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && CheckConnection())
        {
            try
            {
                string path1 = HttpContext.Current.Server.MapPath("~/install/sql/CreateU.sql");
                string path2 = HttpContext.Current.Server.MapPath("~/install/sql/CreateSP.sql");
                string path3 = HttpContext.Current.Server.MapPath("~/install/sql/CreateV.sql");
                string[] strpath ={ path1, path3, path2 };
                for (int i = 0; i < strpath.Length; i++)
                {
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
                    System.IO.StreamReader reader = new System.IO.StreamReader(strpath[i], System.Text.Encoding.GetEncoding("gb2312"));
                    string st = reader.ReadToEnd();
                    SqlCommand com = new SqlCommand(st, con);
                    using (con)
                    {
                        con.Open();
                        com.ExecuteNonQuery();
                    }
                }
                InsertData();
                InsertArea();

            }
            catch(Exception ex)
            {
                this.right1.Visible = true;
                this.right.Visible = false;
                this.labError.InnerHtml = "安装出现错误请重新安装！";
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private bool CheckConnection()
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString.ToString());
        try
        {
            connection.Open();
            if (connection.State != ConnectionState.Open)
            {
                this.right1.Visible = true;
                this.right.Visible = false;
                this.labError.InnerHtml = "<font style='font-size:14px;color:#f00'>数据库连接失败，请检查服务器信息！</font>";
                return false;
            }
        }
        catch
        {
            this.right1.Visible = true;
            this.right.Visible = false;
            this.labError.InnerHtml = "<font style='font-size:14px;color:#f00'>数据库连接失败，请检查服务器信息！</font>";
            return false;
        }
        return true;
    }

    /// <summary>
    /// 
    /// </summary>
    protected void InsertData()
    {
        //插入系统初始化信息
        string mangeName = Request.QueryString["manage"].ToString();
        string password = Request.QueryString["password"].ToString();
        string webName = Request.QueryString["web"].ToString();
        string webUrl = Request.QueryString["url"].ToString();
        string Md5 = Request.QueryString["Md5"].ToString();
        string str1 = "INSERT [W_Manager] ([ManagerName],[PassWord],[RoleID],[Lockedup],[LastIP],[LastDate],[AddDate]) VALUES ( '" + mangeName + "','" + password + "',1,0,'127.0.0.1','2009-11-6 8:49:17','2009-11-6 16:57:51')";
        string str2 = "INSERT  into [W_WebBasic] ([SystemTel],[HotTel],[FaxTel],[CorporationName],[Address],[PostalCode],[Email],[EmailName],[EmailPass],[SMTP],[State],[StopReadme],[WebName],[WebURL],[Catalog],[isBlog],[isSecondURL],[isForum],[Copyright],[WebLOGO],[Filter],[Rewrite],[LoginName],[AdminUserID],[Encryption]) VALUES ( '0135-68348273','0135-68348273','0135-68348273','" + webName + "','" + webUrl + "','034000','websen_4187@sohu.com','websen_4187','websen123','smtp.sohu.com',1,'网站维护中，请稍后访问。。。','" + webName + "','" + webUrl + "','admin/',0,1,0,'dassd','/images/logo.jpg','aa','.aspx','websennet',1,'"+Md5+"')";
        string[] strnum ={ str1, str2 };
        for (int i = 0; i < strnum.Length; i++)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
            SqlCommand com = new SqlCommand(strnum[i], con);
            using (con)
            {
                con.Open();
                com.ExecuteNonQuery();
            }
        }
    }

    protected void InsertArea()
    {
        //插入地区数据和主营行业数据
        string IsImport="";
        string pathInsertValue_ = HttpContext.Current.Server.MapPath("~/install/sql/InsertValue.sql");
        string path_area = HttpContext.Current.Server.MapPath("~/install/sql/InsertArea.sql");
        string SystemValue = HttpContext.Current.Server.MapPath("~/install/sql/InsertSystemValue.sql");
		string Update = HttpContext.Current.Server.MapPath("~/install/sql/Update.sql");
        if (Request.QueryString["IsImport"] != "" && Request.QueryString["IsImport"] != null)
            IsImport = Request.QueryString["IsImport"];
        string[] path_ai ={ path_area, pathInsertValue_ ,SystemValue,Update};
        if (IsImport.Equals("False"))
            path_ai = new string[] { path_area, SystemValue,Update };
        for (int i = 0; i < path_ai.Length; i++)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString);
            System.IO.StreamReader reader = new System.IO.StreamReader(path_ai[i], System.Text.Encoding.GetEncoding("gb2312"));
            string rederString = reader.ReadToEnd();
            SqlCommand com = new SqlCommand(rederString, con);
            using (con)
            {
                con.Open();
                com.ExecuteNonQuery();
            }
        }

    }
}
